package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.constant.FlightOrderConstants;
import com.ktgj.customer.domain.vo.AutobillExceptionVo;
import com.ktgj.customer.domain.vo.BackfillTicketNumberVo;
import com.ktgj.customer.domain.vo.FinanceAutobillExceptionVo;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.FinanceAutobillException;
import com.ktgj.customer.service.IFinanceAutobillExceptionService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 财务自动对账结果Controller
 * 
 * @author ruoyi
 * @date 2022-06-13
 */
@ControllerAdvice
@Component
@RestController
@RequestMapping("/exception")
public class FinanceAutobillExceptionController extends BaseController
{
    private static IFinanceAutobillExceptionService financeAutobillExceptionService;

    @Autowired
    private void setIFinanceAutobillExceptionService(IFinanceAutobillExceptionService financeAutobillExceptionService){
        FinanceAutobillExceptionController.financeAutobillExceptionService = financeAutobillExceptionService;
    }


    /**
     * 查询财务自动对账结果列表
     */
    @PreAuthorize(hasPermi = "customer:exception:list")
    @GetMapping("/list")
    public TableDataInfo list(FinanceAutobillException financeAutobillException)
    {
        startPage();
        List<FinanceAutobillException> list = financeAutobillExceptionService.selectFinanceAutobillExceptionList(financeAutobillException);
        return getDataTable(list);
    }

    /**
     * 导出财务自动对账结果列表
     */
    @PreAuthorize(hasPermi = "customer:exception:export")
    @Log(title = "财务自动对账结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceAutobillException financeAutobillException) throws IOException
    {
        List<FinanceAutobillException> list = financeAutobillExceptionService.selectFinanceAutobillExceptionList(financeAutobillException);
        ExcelUtil<FinanceAutobillException> util = new ExcelUtil<FinanceAutobillException>(FinanceAutobillException.class);
        util.exportExcel(response, list, "财务自动对账结果数据");
    }

    /**
     * 获取财务自动对账结果详细信息
     */
    @PreAuthorize(hasPermi = "customer:exception:query")
    @GetMapping(value = "/{billExceptionId}")
    public AjaxResult getInfo(@PathVariable("billExceptionId") Long billExceptionId)
    {
        return AjaxResult.success(financeAutobillExceptionService.selectFinanceAutobillExceptionById(billExceptionId));
    }

    /**
     * 新增财务自动对账结果
     */
    @PreAuthorize(hasPermi = "customer:exception:add")
    @Log(title = "财务自动对账结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinanceAutobillException financeAutobillException)
    {
        return toAjax(financeAutobillExceptionService.insertFinanceAutobillException(financeAutobillException));
    }

    /**
     * 修改财务自动对账结果
     */
    @PreAuthorize(hasPermi = "customer:exception:edit")
    @Log(title = "财务自动对账结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinanceAutobillException financeAutobillException)
    {
        return toAjax(financeAutobillExceptionService.updateFinanceAutobillException(financeAutobillException));
    }

    /**
     * 删除财务自动对账结果
     */
    @PreAuthorize(hasPermi = "customer:exception:remove")
    @Log(title = "财务自动对账结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billExceptionIds}")
    public AjaxResult remove(@PathVariable Long[] billExceptionIds)
    {
        return toAjax(financeAutobillExceptionService.deleteFinanceAutobillExceptionByIds(billExceptionIds));
    }

    /**
     * 对账结果列表
     * */
    @PostMapping("/financeAutobillExceptionList")
    public TableDataInfo financeAutobillExceptionList(@RequestBody FinanceAutobillExceptionVo financeAutobillExceptionVo) {
        startPage();
        List<FinanceAutobillException> financeAutobillExceptions = financeAutobillExceptionService.financeAutobillExceptionList(financeAutobillExceptionVo);
        return getDataTable(financeAutobillExceptions);
    }

    /**
     * 回填票号
     * */
    @PostMapping("/backfillTicketNumber")
    public AjaxResult backfillTicketNumber(@RequestBody BackfillTicketNumberVo backfillTicketNumberVo){
        int i;
        try {
            i = financeAutobillExceptionService.backfillTicketNumber(backfillTicketNumberVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("回填成功");
        }
        else {
            return AjaxResult.error("回填失败");
        }
    }

    /**
     * 对账调整列表
     * */
    @PostMapping("/reconciliationAdjustment")
    private TableDataInfo reconciliationAdjustment(@RequestBody AutobillExceptionVo autobillExceptionVo){
        startPage();
        try {
            List<FinanceAutobillException> financeAutobillExceptionList = financeAutobillExceptionService.reconciliationAdjustment(autobillExceptionVo);
            return getDataTable(financeAutobillExceptionList);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 修改状态为票号重复
     * */
    @PostMapping("/ticketNumRepeat")
    private AjaxResult ticketNumRepeat(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        int i;
        try {
            i = financeAutobillExceptionService.ticketNumRepeat(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }
}
